app.factory('configReader', ['$http', function configReaderFactory($http) {
  var cache;

  function read(index, ignoreCache) {
    var def = jQuery.Deferred();

    if (cache && !ignoreCache) {
      var config = cache[index] || cache;
      def.resolve(config);
    } else {
      $http.get('/config')
        .success(function(data) {
          cache = data;
          var config = index ? cache[index] : cache;
          def.resolve(config);
        })
        .error(function(data, status) {
          def.reject(data, status);
        });
    }

    return def.promise();
  }

  return {
    read: read
  };
}]);
